package com.chaofan.sell.controller;

import com.chaofan.sell.dto.OrderDTO;
import com.chaofan.sell.enums.Result;
import com.chaofan.sell.exception.SellException;
import com.chaofan.sell.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 卖家端订单
 *
 * @author Fairy
 * @date 2021/9/22
 */
@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 卖家端查询订单
     *
     * @param page 查询页
     * @param size 每页数量
     * @param map  map
     * @return view
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(pageRequest);
        map.put("orderDTOPage", orderDTOPage);
        map.put("currentPage", page);
        map.put("size", size);
        log.info("【卖家端】查询订单列表");
        return new ModelAndView("order/list", map);
    }

    /**
     * 卖家端取消订单
     *
     * @param orderId 订单id
     * @return view
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId, Map<String, Object> map) {
        try {
            OrderDTO one = orderService.findOne(orderId);
            orderService.cancel(one);
        } catch (SellException e) {
            log.error("【卖家端取消订单】发生异常, exception={}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("error", map);
        }

        map.put("msg", Result.ORDER_CANCEL_SUCCESS.getMsssage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("success");
    }

    /**
     * 卖家端订单详情
     *
     * @param orderId 订单id
     * @return view
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId, Map<String, Object> map) {
        OrderDTO one = new OrderDTO();
        try {
            one = orderService.findOne(orderId);
        } catch (SellException e) {
            log.error("【卖家端查看订单详情】发生异常, exception={}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("error", map);
        }
        map.put("orderDTO", one);
        return new ModelAndView("order/detail", map);
    }

    /**
     * 完结订单
     *
     * @param orderId 订单id
     * @return view
     */
    @GetMapping("/finish")
    public ModelAndView finished(@RequestParam("orderId") String orderId, Map<String, Object> map) {
        try {
            OrderDTO one = orderService.findOne(orderId);
            orderService.finish(one);
        } catch (SellException e) {
            log.error("【卖家端完结订单】发生异常, exception={}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("error", map);
        }

        map.put("msg", Result.ORDER_FINISH_SUCCESS.getMsssage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("success");
    }
}
